#include <cstdio>
#include <cstdlib>
//#include <tchar.h>

#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/video/video.hpp"
#include <opencv2/gpu/gpu.hpp>

#include "Classifier.h"

#include "MotionAnalysis.h"

int main(int argc, char * const argv[])
{
	string vidfile = "./../_FGS/vid/VHand.avi";

	int start_frame= 100;
	int end_frame = 100+21;
	double scale = 0.5;

	int visual = 0;

	LcLowRank my_subtractor;
	//LcProjRANSAC my_subtractor;

	TrajPixelLabeling mylabeler( LC_RGB , new LcRandomTrees() );
	//TrajPixelLabeling mylabeler( LC_RGB , new LcKNN() );

	//===================================

	vector<Mat> imgs; imgs.clear();
	Mat key_frame;

	VideoCapture cap(vidfile);
	
	cout << "read video" << endl;

	for(int i = 0 ; i< start_frame;i++){Mat temp;cap >> temp;}

	for(int i = start_frame;i<end_frame;i++)
		{ Mat temp; cap>>temp;resize( temp, temp, Size(), scale,scale); imgs.push_back( temp);}

	imgs[(end_frame-start_frame-1)/2].copyTo( key_frame );

	Mat trajs;
	vector<Point2f> points;

	{
		//get trajectories at first
		LcKLT myKLT;
		myKLT.work( imgs, trajs, &points);
	}

	vector<int> inlier,outlier,base_id;

	{
		//doing trajectories subtra
		my_subtractor.work(trajs, inlier, outlier, &base_id);

		if(visual)
		{
			Mat dsp; key_frame.copyTo(dsp);
			my_subtractor.draw( dsp, points, inlier, outlier, &base_id);
			imshow("debug", dsp);
			cv::waitKey(100);
		}
	}


	
	Mat result;
	mylabeler.work( points, key_frame, inlier, outlier,result);

	if(visual)
	{
		imshow("result",result);
		cv::waitKey(0);
	}
	else
	{
		imwrite("result.jpg",result);
	}
	return 0;
}